//
//  ASR.h
//  ASR
//
//  Created by WanKang on 7/9/16.
//  Copyright © 2016 WanKang. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ASRWordScore : NSObject
@property (nonatomic) NSNumber *mBeginTime;
@property (nonatomic) NSNumber *mEndTime;
@property (nonatomic) NSInteger mScore;
@property (nonatomic) NSInteger mReason;
@property (nonatomic) NSString *mWord;
@end

@interface ASRPinyinScore : NSObject
@property (nonatomic) NSNumber *mBeginTime;
@property (nonatomic) NSNumber *mEndTime;
@property (nonatomic) NSInteger mWordIndex;
@property (nonatomic) NSInteger mScore;
@property (nonatomic) NSString *mPinYin;
@end

@interface ASRPhoneScore : NSObject
@property (nonatomic) NSNumber *mBeginTime;
@property (nonatomic) NSNumber *mEndTime;
@property (nonatomic) NSInteger mWordIndex;
@property (nonatomic) NSInteger mPinyinIndex;
@property (nonatomic) NSInteger mScore;
@property (nonatomic) NSString *mPhone;
@end

@interface ASREngine : NSObject
+(NSInteger)initEngine:(NSData *)pData;
+(NSInteger)uninitEngine;
@end

#define ASR_PARM_MAX_SPEECH               (0x0001)       //in Milliseconds
#define ASR_PARM_MAX_BEGIN_SIL            (0x0002)       //in Milliseconds
#define ASR_PARM_MAX_END_SIL              (0x0003)       //in Milliseconds

/** main class to call ASR
 */
@interface ASR : NSObject
-(id)init;
/** to release inner engine HANDLE automatically
 */
-(void)dealloc;
/** create engine with a given utterance folder and license string
 * @param sFolder [in] the given folder to save utterance
 * @param sLicense [in] the license string, should not be empty
 */
-(BOOL)createWithFolder:(NSString *)sFolder license:(NSString *)sLicence;
/** to release inner engine HANDLE
 */
-(BOOL)destroy;
-(BOOL)start;
-(BOOL)stop;
-(BOOL)reset;
/** should be called to input speech data
 * @param pnData [in] data in short PCM format
 * @param nCount count of pnData, should be larger than 0
 * @note please note that pnData may be changed.
 */
-(NSInteger)setData:(short *)pnData count:(NSInteger)nCount;
-(BOOL)setUtteranceOption:(NSInteger)nData tag:(NSString *)sTag;
-(BOOL)setParameter:(NSInteger)nParam value:(NSInteger)nValue;
/** return value NSNumber* is type of float, in 10th millisecond(0.1ms)
 */
-(NSNumber *)getSpeechBegin;
/** return value NSNumber* is type of float, in 10th millisecond(0.1ms)
 */
-(NSNumber *)getSpeechEnd;
-(NSInteger)getConfidence;
-(NSInteger)getWordNum;
-(NSInteger)getWordScore:(NSInteger)nIndex;
/** return value NSNumber* is type of float, in 10th millisecond(0.1ms)
 */
-(NSNumber *)getWordBeginTime:(NSInteger)nIndex;
/** return value NSNumber* is type of float, in 10th millisecond(0.1ms)
 */
-(NSNumber *)getWordEndTime:(NSInteger)nIndex;
-(NSInteger)getResultCount;
-(NSInteger)loadGrammar:(NSString *)sGrammar langID:(NSInteger)nLangID isPinYin:(BOOL)fPinYin;
-(NSString *)getWordStr:(NSInteger)nIndex;
-(NSString *)getFirstWordStr;
-(NSString *)getUtterancePath;
-(BOOL)openGrammar:(NSString *)sFile;
-(BOOL)closeGrammar;
-(BOOL)activateGrammar;
-(BOOL)saveGrammar:(NSString *)sFile;
/** to add a grammar
 * @nLangID 0x0001 for English; 0x0002 for simplified Chinese; 0x0004 for traditional Chinese(not supported yet);
 * may be a combine(i.e. 0x0003); should not be 0
 * @param fPinYin whether this is a pinyin string or not; if there is pinyin in string, nLangID should be specified(either 2 or 3).
 */
-(BOOL)addGrammar:(NSString *)sGrammar option:(NSInteger)nOption langID:(NSInteger)nLangID isPinYin:(BOOL)fPinYin;
/** to return array of ASRWordScore
 */
-(NSArray *)getWordScoreDetail;
/** to return array of ASRPinyinScore
 */
-(NSArray *)getPinyinScoreDetail;
/** to return array of ASRPhoneScore
 */
-(NSArray *)getPhoneScoreDetail;
/** to get current ASR library version
 * current version string is "Creative.AsrEngine.iOS.0.5.20160907"
 */
+(NSString *)getVersion;
@end

/*****************************************************************************/
#define SPEECH_ENGINE_ASR_CALL_FAIL (-1)
#define SPEECH_ENGINE_NOT_SETUP_YET (-2)
#define SPEECH_ENGINE_GRAMMAR_CREATE_FAIL (-3)
#define SPEECH_ENGINE_GRAMMAR_NOT_FOUND (-4)
#define SPEECH_ENGINE_QUERY_ERROR (-5)
#define SPEECH_ENGINE_RECOG_ERROR (-6)
#define SPEECH_ENGINE_RESULT_NOSPEECH (-7)
#define SPEECH_ENGINE_RESULT_NORESULT (-8)
#define SPEECH_ENGINE_RESULT_OTHERS (-9)

/*****************************************************************************/

/** simplified calling of ASR
 */
@interface SimpleASR : NSObject
-(id)initWithLicense:(NSString *)sLicence;
-(void)dealloc;
/** to create a grammar, with language-auto-detection
 */
-(NSInteger)createGrammar:(NSString *)grmName items:(NSArray *)grmItem;
/** to create a new grammar
 * @nLangID 0x0001 for English; 0x0002 for simplified Chinese; 0x0004 for traditional Chinese(not supported yet);
 * may be a combine(i.e. 0x0003); 0 means to check automatically
 * @param fPinYin whether this is a pinyin string or not; if there is pinyin in string, nLangID should be specified(either 2 or 3).
 */
-(NSInteger)createGrammar:(NSString *)grmName items:(NSArray *)grmItem option:(NSInteger)nOption langID:(NSInteger)nLangID isPinYin:(BOOL)fPinYin;
-(NSInteger)setup:(NSInteger)nBest;
-(NSInteger)recognize:(const unsigned char *)bData dataSize:(NSInteger)nSize;
-(NSInteger)startRecog:(NSString *)grmName;
-(NSInteger)stopRecog;
/** clear grammar and release resources
 */
-(NSInteger)destroy;
/** idxScore will be filled with NSNumber*(type is intValue; LOWORD, index; HIWORD, score)
  */
-(NSInteger)queryResultIndex:(NSMutableArray *)idxScore;
@end
